var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsDetail:[],     // 商品数据
        type:1,             // 商品状态   1：售卖中  2：已售卖
    },
    soldOut(){
        this.dialog.showDialog();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.dialog = this.selectComponent("#dialog");

        var self = this;
        if (options.id) {
            wx.getStorage({
                key: 'user',
                success: function(res) {
                    self.setData({
                        userData: res.data,
                        type: options.type,
                    })
                    wx.request({
                        url: app.url.requestUrl + '/goods/goodsdetails',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                            id: options.id
                        },
                        success: function(data) {
                            var list = data.data;
                            if (list.code == 1) {
                                self.setData({
                                    goods_id: options.id,
                                    goodsDetail: list.data,
                                })
                            }
                        }
                    })
                },
            })
        }
    },
    // 取消按钮
    __cancelEvent(){
        this.dialog.hideDialog();
    },
    // 确认按钮
    __confirmEvent(){
        var self = this;
        var goods_id = parseInt(this.data.goodsDetail.id);
        wx.getStorage({
            key: 'user',
            success: function (res) {
                self.setData({
                    userData: res.data
                })
                wx.request({
                    url: app.url.requestUrl + '/personal/downshel',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: goods_id
                    },
                    success: function (data) {
                        var list = data.data;
                        if (list.code == 1) {
                            self.dialog.hideDialog();
                            wx.showLoading({
                                title: list.msg,
                            })
                            setTimeout(function(){
                                wx.navigateTo({
                                    url: '../my-baby/my-baby',
                                })
                            })

                        } else {
                            wx.showToast({
                                image: '../../img/warning.png',
                                title: list.msg,
                            })
                        }
                    }
                })
            },
        })
    }
})